<?php 
	include_once 'dataservice.php';
	include_once 'thuvienham.php';

	class nguoidung_db 
	{
		public function Them($MaNguoiDung, $MaLoaiNguoiDung, $MaChucVu, $HoTen, $NgaySinh, $DiaChi, $DienThoai, $Email, $MatKhau, $DangNhap2, $MatKhau2, $Avatar, $PIN)
		{
			$sql = "INSERT INTO `tbl_nguoidung`(`MaNguoiDung`, `MaLoaiNguoiDung`, `MaChucVu`, `HoTen`, `NgaySinh`, `DiaChi`, `DienThoai`, `Email`, `MatKhau`, `DangNhap2`, `MatKhau2`, `Avatar`, `PIN`)
					VALUES ('$MaNguoiDung', '$MaLoaiNguoiDung', '$MaChucVu', '$HoTen', '$NgaySinh', '$DiaChi', '$DienThoai', '$Email', '$MatKhau', '$DangNhap2', '$MatKhau2', '$Avatar', '$PIN')";

			$service = new dataservice();
			return $service->ExecuteNonQuery($sql);
		}

		public function Xoa($MaNguoiDung)
		{
			$sql = "DELETE FROM `tbl_nguoidung` WHERE `MaNguoiDung` = '$MaNguoiDung'";

			$service = new dataservice();
			return $service->ExecuteNonQuery($sql);
		}

		public function CapNhat($MaNguoiDung, $MaLoaiNguoiDung, $MaChucVu, $HoTen, $NgaySinh, $DiaChi, $DienThoai, $Email, $MatKhau, $DangNhap2, $MatKhau2, $Avatar, $PIN)
		{
			$sql = "UPDATE `tbl_nguoidung` SET ";
			$tmp = 0;
			if($MaLoaiNguoiDung != "")
			{
				$tmp = 1;
				$sql .= "  `MaLoaiNguoiDung` = '$MaLoaiNguoiDung' ";
			}
			if($MaChucVu != "")
			{
				$t = ($tmp == 1) ? " , " : "" ;
				$tmp = 1;
				$sql .= $t . " `MaChucVu` = '$MaChucVu' ";
			}
			if($HoTen != "")
			{
				$t = ($tmp == 1) ? " , " : "" ;
				$tmp = 1;
				$sql .= $t . " `HoTen` = '$HoTen' ";
			}

			if($NgaySinh != "0000-00-00")
			{
				$t = ($tmp == 1) ? " , " : "" ;
				$tmp = 1;
				$sql .= $t . " `NgaySinh` = '$NgaySinh' ";
			}

			if($DiaChi != "")
			{
				$t = ($tmp == 1) ? " , " : "" ;
				$tmp = 1;
				$sql .= $t . " `DiaChi` = '$DiaChi' ";
			}

			if($DienThoai != "")
			{
				$t = ($tmp == 1) ? " , " : "" ;
				$tmp = 1;
				$sql .= $t . " `DienThoai` = '$DienThoai' ";
			}

			if($Email != "")
			{
				$t = ($tmp == 1) ? " , " : "" ;
				$tmp = 1;
				$sql .= $t . " `Email` = '$Email' ";
			}

			if($MatKhau != "")
			{
				$t = ($tmp == 1) ? " , " : "" ;
				$tmp = 1;
				$sql .= $t . " `MatKhau` = '$MatKhau' ";
			}

			if($DangNhap2 != -1)
			{
				$t = ($tmp == 1) ? " , " : "" ;
				$tmp = 1;
				$sql .= $t . " `DangNhap2` = '$DangNhap2' ";
			}

			if($MatKhau2 != "")
			{
				$t = ($tmp == 1) ? " , " : "" ;
				$tmp = 1;
				$sql .= $t . " `MatKhau2` = '$MatKhau2' ";
			}

			if($Avatar != "")
			{
				$t = ($tmp == 1) ? " , " : "" ;
				$tmp = 1;
				$sql .= $t . " `Avatar` = '$Avatar' ";
			}
			if($PIN != "")
			{
				$t = ($tmp == 1) ? " , " : "" ;
				$tmp = 1;
				$sql .= $t . " `PIN` = '$PIN' ";
			}
			$sql .= " WHERE `MaNguoiDung` = '$MaNguoiDung'";
			$service = new dataservice();
			return $service->ExecuteNonQuery($sql);
		}
		public function CapNhatMatKhau($MaNguoiDung, $MatKhau)
		{
			$sql = "UPDATE `tbl_nguoidung`
					SET `MatKhau` = '$MatKhau'
					WHERE `MaNguoiDung` = '$MaNguoiDung'";

			$service = new dataservice();
			return $service->ExecuteNonQuery($sql);
		}
		public function CapNhatMatKhau2($MaNguoiDung, $MatKhau2)
		{
			$sql = "UPDATE `tbl_nguoidung`
					SET `MatKhau2` = '$MatKhau2'
					WHERE `MaNguoiDung` = '$MaNguoiDung'";

			$service = new dataservice();
			return $service->ExecuteNonQuery($sql);
		}
		
		public function CapNhatDangNhap2Buoc($MaNguoiDung, $DangNhap2)
		{
			$sql = "UPDATE `tbl_nguoidung`
					SET `DangNhap2` = '$DangNhap2'
					WHERE `MaNguoiDung` = '$MaNguoiDung'";

			$service = new dataservice();
			return $service->ExecuteNonQuery($sql);
		}
		
		public function CapNhatPIN($MaNguoiDung, $PIN)
		{
			$sql = "UPDATE `tbl_nguoidung`
					SET `PIN` = '$PIN'
					WHERE `MaNguoiDung` = '$MaNguoiDung'";

			$service = new dataservice();
			return $service->ExecuteNonQuery($sql);
		}
		
		public function LayDanhSach($MaNguoiDung, $MaLoaiNguoiDung, $MaChucVu, $HoTen, $NgaySinh, $DiaChi, $DienThoai, $Email, $MatKhau, $DangNhap2, $MatKhau2, $Avatar, $PIN)
		{
			$sql = "SELECT * FROM `tbl_nguoidung` WHERE 1  ";
			if($MaNguoiDung != "")
				$sql .= " AND `MaNguoiDung` = '$MaNguoiDung' ";

			if($MaLoaiNguoiDung != "")
				$sql .= " AND `MaLoaiNguoiDung` = '$MaLoaiNguoiDung' ";

			if($MaChucVu != "")
				$sql .= " AND `MaChucVu` = '$MaChucVu' ";

			if($HoTen != "")
				$sql .= " AND `HoTen` LIKE '%$HoTen%' ";

			if($NgaySinh != "0000-00-00")
				$sql .= " AND `NgaySinh` = '$NgaySinh' ";

			if($DiaChi != "")
				$sql .= " AND `DiaChi` = '$DiaChi' ";

			if($DienThoai != "")
				$sql .= " AND `DienThoai` = '$DienThoai' ";

			if($Email != "")
				$sql .= " AND `Email` = '$Email' ";

			if($MatKhau != "")
				$sql .= " AND `MatKhau` = '$MatKhau' ";

			if($DangNhap2 != -1)
				$sql .= " AND `DangNhap2` = '$DangNhap2' ";

			if($MatKhau2 != "")
				$sql .= " AND `MatKhau2` = '$MatKhau2' ";

			if($Avatar != "")
				$sql .= " AND `Avatar` = '$Avatar' ";

			if($PIN != "")
				$sql .= " AND `PIN` = '$PIN' ";
			
			$service = new dataservice();
			return $service->ExecuteQuery($sql);
		}
		
		public function LayDanhSachPaging($MaNguoiDung, $MaLoaiNguoiDung, $MaChucVu, $HoTen, $NgaySinh, $DiaChi, $DienThoai, $Email, $MatKhau, $DangNhap2, $MatKhau2, $Avatar, $PIN, $Start, $Limit)
		{
			$sql = "SELECT * FROM `tbl_nguoidung` WHERE 1  ";
			if($MaNguoiDung != "")
				$sql .= " AND `MaNguoiDung` = '$MaNguoiDung' ";

			if($MaLoaiNguoiDung != "")
				$sql .= " AND `MaLoaiNguoiDung` = '$MaLoaiNguoiDung' ";

			if($MaChucVu != "")
				$sql .= " AND `MaChucVu` = '$MaChucVu' ";

			if($HoTen != "")
				$sql .= " AND `HoTen` LIKE '%$HoTen%' ";

			if($NgaySinh != "0000-00-00")
				$sql .= " AND `NgaySinh` = '$NgaySinh' ";

			if($DiaChi != "")
				$sql .= " AND `DiaChi` = '$DiaChi' ";

			if($DienThoai != "")
				$sql .= " AND `DienThoai` = '$DienThoai' ";

			if($Email != "")
				$sql .= " AND `Email` = '$Email' ";

			if($MatKhau != "")
				$sql .= " AND `MatKhau` = '$MatKhau' ";

			if($DangNhap2 != -1)
				$sql .= " AND `DangNhap2` = '$DangNhap2' ";

			if($MatKhau2 != "")
				$sql .= " AND `MatKhau2` = '$MatKhau2' ";

			if($Avatar != "")
				$sql .= " AND `Avatar` = '$Avatar' ";

			if($PIN != "")
				$sql .= " AND `PIN` = '$PIN' ";
			$sql .= " LIMIT $Start, $Limit ";
			$service = new dataservice();
			return $service->ExecuteQuery($sql);
		}
		
	}

